package org.tigris.atlas.examples.persist;

import java.util.Set;

import junit.framework.TestCase;

import org.tigris.atlas.examples.service.OneToManySelfRelationServiceResponse;
import org.tigris.atlas.examples.service.delegate.EntityMaintenanceServiceDelegate;
import org.tigris.atlas.examples.service.delegate.ServiceDelegateFactory;
import org.tigris.atlas.examples.transfer.OneToManySelfRelation;
import org.tigris.atlas.examples.transfer.TransferObjectFactory;

public class TestSelf1MRelation extends TestCase {
	
	private EntityMaintenanceServiceDelegate delegate;

	/**
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();
		
		delegate = ServiceDelegateFactory.createEntityMaintenanceServiceDelegate();
		
	}

	/**
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
		super.tearDown();
		
		delegate = null;
		
	}

	public void testParentOnlySave() throws Exception { 
		OneToManySelfRelation parent = TransferObjectFactory.createOneToManySelfRelation();
		
		OneToManySelfRelationServiceResponse response = delegate.save(parent);
		
		assertFalse("No error messsages should have been returned!", response.getMessages().hasErrorMessages());
		OneToManySelfRelation returnedParent = response.getOneToManySelfRelation();
		assertNotNull("A object should have been returned!", returnedParent);
		
	}
	
	public void testParentWithSingleChildSave() throws Exception { 
		OneToManySelfRelation parent = TransferObjectFactory.createOneToManySelfRelation();
		OneToManySelfRelation child = TransferObjectFactory.createOneToManySelfRelation();
		parent.addOneToManySelfRelation(child);
		
		OneToManySelfRelationServiceResponse response = delegate.save(parent);
		
		assertFalse("No error messsages should have been returned!", response.getMessages().hasErrorMessages());
		OneToManySelfRelation returnedParent = response.getOneToManySelfRelation();
		Set childSet = returnedParent.getOneToManySelfRelations();
		assertEquals("Exactly one child should exist!", childSet.size(), 1);
		
	}	
	
	public void testParentWithThreeChildrenSave() throws Exception { 
		OneToManySelfRelation parent = TransferObjectFactory.createOneToManySelfRelation();
		OneToManySelfRelation child0 = TransferObjectFactory.createOneToManySelfRelation();
		parent.addOneToManySelfRelation(child0);
		OneToManySelfRelation child1 = TransferObjectFactory.createOneToManySelfRelation();
		parent.addOneToManySelfRelation(child1);
		OneToManySelfRelation child2 = TransferObjectFactory.createOneToManySelfRelation();
		parent.addOneToManySelfRelation(child2);
		
		OneToManySelfRelationServiceResponse response = delegate.save(parent);
		
		assertFalse("No error messsages should have been returned!", response.getMessages().hasErrorMessages());
		OneToManySelfRelation returnedParent = response.getOneToManySelfRelation();
		Set childSet = returnedParent.getOneToManySelfRelations();
		assertEquals("Exactly three children should exist!", childSet.size(), 3);
		
	}	
	
	public void testChildDeletion() throws Exception { 
		OneToManySelfRelation parent = TransferObjectFactory.createOneToManySelfRelation();
		OneToManySelfRelation child0 = TransferObjectFactory.createOneToManySelfRelation();
		parent.addOneToManySelfRelation(child0);
		OneToManySelfRelation child1 = TransferObjectFactory.createOneToManySelfRelation();
		parent.addOneToManySelfRelation(child1);
		OneToManySelfRelation child2 = TransferObjectFactory.createOneToManySelfRelation();
		parent.addOneToManySelfRelation(child2);
		
		OneToManySelfRelationServiceResponse response = delegate.save(parent);
		
		assertFalse("No error messsages should have been returned!", response.getMessages().hasErrorMessages());
		OneToManySelfRelation returnedParent = response.getOneToManySelfRelation();
		Set childSet = returnedParent.getOneToManySelfRelations();
		assertEquals("Exactly three children should exist!", childSet.size(), 3);
		
		OneToManySelfRelation returnedChild = (OneToManySelfRelation)childSet.iterator().next();
		returnedParent.removeOneToManySelfRelation(returnedChild);
		
		OneToManySelfRelationServiceResponse afterDeletionResponse = delegate.save(returnedParent);
		assertFalse("No error messsages should have been returned!", afterDeletionResponse.getMessages().hasErrorMessages());
		OneToManySelfRelation afterDeletionParent = afterDeletionResponse.getOneToManySelfRelation();
		childSet = afterDeletionParent.getOneToManySelfRelations();
		assertEquals("Exactly two children should exist!", childSet.size(), 2);
		
	}	
	
}
